# -*- coding: utf-8 -*-
"""
Created on Tue Nov 05 18:00:51 2013

@author: Maxim
"""
from numpy import array, zeros, linspace, arange
from paths import CFD_paths
import airfoil
from os import system


def get_file_contents(path):
    fid = open(path,'rt')
    lines = fid.readlines()
    fid.close()
    return lines

class ScriptFile():
    def __init__(self,templateFile,outputFile):
        fid1 = open(templateFile,'rt')
        self.template = fid1.readlines()
        self.fid = open(outputFile,'wt')
    def write(self,line):
        self.fid.write(line)
    def write_template_lines(self,lineNo):
        for n in lineNo:
            self.write(self.template[n])
    def close(self):
        self.fid.close()

class AirfoilCMesh():
    def __init__(self,airfoil, xfarfield=20.0, yfarfield=15.0, afPts=75, 
                 xfarfieldPts=50, yfarfieldPts=50, dsWall=1e-4,dSfarfield=1.0,
                 dsLeadingEdge=1e-3,dsTrailingEdge=1e-3):
        self.af = airfoil
        self._xfarfield = 20.0
        self._yfarfield = 15.0
        self._airfoilPts = 100
        self._xfarfieldPts = 100
        self._yfarfieldPts = 100
        self._wallSpacing = 1e-5
        self._farfieldSpacing = 1.0
        self._leSpacing = 1e-3
        self._teSpacing = 1e-3
        self._frontFarfieldSpacing = 1e-2
    
    def create(self,meshFilePath,caseFilePath):
        self._create_script(meshFilePath,caseFilePath)
        system('\"%s\"'%meshFilePath)
    
    def _create_script(self,outputFile,caseFilePath,templateFile=None,batch=True):
        paths = CFD_paths()
        if templateFile==None:
            templateFile = paths.template_pw2
        script = ScriptFile(templateFile,outputFile)
        script.write('# Airfoil Cmesh script for Pointwise V16.03R2\n#generated by Maxim Tyan\n')
        script.write_template_lines(arange(0,8))
        for pt in self.af.upPts:
            script.write('$_TMP(PW_1) addPoint {%.6f %.6f 0}\n'%(pt[0],pt[1]))
        script.write_template_lines(arange(10,23))
        for pt in self.af.loPts[1:-1]:
            script.write('$_TMP(PW_2) addPoint {%.6f %.6f 0}\n'%(pt[0],pt[1]))
        script.write_template_lines(arange(24,45))
        script.write('$_TMP(PW_4) addPoint [pwu::Vector3 add [$_CN(1) getPosition -arc 1] {%.4f 0 0}]\n'%self._xfarfield)
        script.write_template_lines(arange(46,58))
        script.write('$_TMP(PW_5) addPoint [pwu::Vector3 add [$_CN(3) getPosition -arc 1] {0 %.4f 0}]\n'%self._yfarfield)
        script.write_template_lines(arange(59,72))
        script.write('$_TMP(PW_6) addPoint [pwu::Vector3 add [$_CN(4) getPosition -arc 1] {-%.4f 0 0}]\n'%self._xfarfield)
        script.write_template_lines(arange(73,85))
        script.write('$_TMP(PW_7) addPoint [pwu::Vector3 add [$_CN(3) getPosition -arc 1] {0 -%.4f 0}]\n'%self._yfarfield)
        script.write_template_lines(arange(86,99))
        script.write('$_TMP(PW_8) addPoint [pwu::Vector3 add [$_CN(5) getPosition -arc 1] {-%.4f 0 0}]\n'%self._xfarfield)
        script.write_template_lines(arange(100,137))
        script.write('$_TMP(PW_12) do setDimension %d\n'%self._xfarfieldPts)
        script.write_template_lines(arange(138,144))
        script.write('$_TMP(PW_13) do setDimension %d\n'%self._yfarfieldPts)
        script.write_template_lines(arange(145,151))
        script.write('$_TMP(PW_14) do setDimension %d\n'%self._airfoilPts)
        script.write_template_lines(arange(152,159))
        script.write('$_TMP(PW_15) do setDimension %d\n'%(2*self._airfoilPts-1))
        script.write_template_lines(arange(160,166))
        script.write('$_TMP(PW_16) setBeginSpacing %.10f\n'%self._leSpacing)
        script.write_template_lines(arange(167,169))
        script.write('$_TMP(PW_17) setBeginSpacing %.10f\n'%self._leSpacing)
        script.write_template_lines(arange(170,177))
        script.write('$_TMP(PW_18) setEndSpacing %.10f\n'%self._teSpacing)
        script.write_template_lines(arange(178,180))
        script.write('$_TMP(PW_19) setEndSpacing %.10f\n'%self._teSpacing)
        script.write_template_lines(arange(181,183))
        script.write('$_TMP(PW_20) setBeginSpacing %.10f\n'%self._teSpacing)
        script.write_template_lines(arange(184,191))
        script.write('$_TMP(PW_21) setBeginSpacing %.10f\n'%(5.*self._teSpacing))
        script.write_template_lines(arange(192,194))
        script.write('$_TMP(PW_22) setEndSpacing %.10f\n'%(5.*self._teSpacing))
        script.write_template_lines(arange(195,197))
        script.write('$_TMP(PW_23) setEndSpacing %.10f\n'%(5.*self._teSpacing))
        script.write_template_lines(arange(198,205))
        script.write('$_TMP(PW_24) setBeginSpacing %.10f\n'%self._frontFarfieldSpacing)
        script.write_template_lines(arange(206,208))
        script.write('$_TMP(PW_25) setEndSpacing %.10f\n'%self._frontFarfieldSpacing)
        script.write_template_lines(arange(209,216))
        script.write('$_TMP(PW_26) setBeginSpacing %.10f\n'%self._wallSpacing)
        script.write_template_lines(arange(217,219))
        script.write('$_TMP(PW_27) setBeginSpacing %.10f\n'%self._wallSpacing)
        script.write_template_lines(arange(220,227))
        script.write('$_TMP(PW_28) setEndSpacing %.10f\n'%self._farfieldSpacing)
        script.write_template_lines(arange(228,230))
        script.write('$_TMP(PW_29) setBeginSpacing %.10f\n'%self._farfieldSpacing)
        script.write_template_lines(arange(231,233))
        script.write('$_TMP(PW_30) setEndSpacing %.10f\n'%self._farfieldSpacing)
        script.write_template_lines(arange(234,236))
        script.write('$_TMP(PW_31) setBeginSpacing %.10f\n'%self._farfieldSpacing)
        script.write_template_lines(arange(237,244))
        script.write('$_TMP(PW_32) setEndSpacing %.10f\n'%self._farfieldSpacing)
        script.write_template_lines(arange(245,308))
        script.write('pw::Application export [list $_DM(1)] {%s}\n'%caseFilePath)
        if batch:
            script.write('pw::Application exit 0')
        script.close()

class AirfoilOMesh():
    def __init__(self,airfoil,afPts=75,interiorPts=100,dsWall=1e-4,dsLE=1e-3,dsTE=5e-4,growthRate=1.1):
        self.af = airfoil
        self._airfoilPts = afPts
        self._interiorPts = interiorPts
        self._dsWall = dsWall
        self._dsLE = dsLE
        self._dsTE = dsTE
        self._growthRate = growthRate
        

    def create(self,meshFilePath,caseFilePath,batch=True):
        self._create_script(meshFilePath,caseFilePath,batch=batch)
        system('\"%s\"'%meshFilePath)

    def _create_script(self,outputFile,caseFilePath,templateFile=None,batch=True):
        paths = CFD_paths()
        if templateFile==None:
            templateFile = paths.template_pw3
        script = ScriptFile(templateFile,outputFile)
        script.write('# Airfoil Cmesh script for Pointwise V16.03R2\n#generated by Maxim Tyan\n')
        script.write_template_lines(arange(0,8))
        for pt in self.af.upPts:
            script.write('$_TMP(PW_1) addPoint {%.6f %.6f 0}\n'%(pt[0],pt[1]))
        script.write_template_lines(arange(10,23))
        for pt in self.af.loPts[1:-1]:
            script.write('$_TMP(PW_2) addPoint {%.6f %.6f 0}\n'%(pt[0],pt[1]))
        script.write_template_lines(arange(24,38))
        script.write('$_TMP(PW_3) do setDimension %d\n'%self._airfoilPts)
        script.write_template_lines(arange(39,45))
        script.write('$_TMP(PW_4) setBeginSpacing %.8f\n'%self._dsLE)
        script.write_template_lines(arange(46,48))
        script.write('$_TMP(PW_5) setBeginSpacing %.8f\n'%self._dsLE)
        script.write_template_lines(arange(49,56))
        script.write('$_TMP(PW_6) setEndSpacing %.8f\n'%self._dsTE)
        script.write_template_lines(arange(57,59))
        script.write('$_TMP(PW_7) setEndSpacing %.8f\n'%self._dsTE)
        script.write_template_lines(arange(60,76))
        script.write('$_DM(1) setExtrusionSolverAttribute NormalInitialStepSize %.8f\n'%self._dsWall)
        script.write('$_DM(1) setExtrusionSolverAttribute SpacingGrowthFactor %.8f\n'%self._growthRate)
        script.write('$_TMP(mode_6) run -keepFailingStep %d\n'%self._interiorPts)
        script.write_template_lines(arange(78,111))
        appendix = get_file_contents(paths.template_pw4)
        appendix[18] = '  $_TMP(mode_1) initialize -type CAE {%s}\n'%caseFilePath
        for line in appendix:
            script.write(line)
        #script.write('pw::Application export [list $_DM(1)] {%s}\n'%caseFilePath)
        if batch:
            script.write('pw::Application exit 0')
        script.close()

def run_debug1():
    af = airfoil.Airfoil()
    #af.naca4(12,3,40,nPts=50)
    af.read_txt('RAE2822_cst.txt')
    
    mesh = AirfoilOMesh(af)
    mesh.create('debug_glf_journal.glf','debug_cas_file.cas',batch=False)

if __name__=="__main__":
    run_debug1()